/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wat.serverapp.remote;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import wat.ziszcommon.remote.CryptoUtils;
import wat.ziszcommon.remote.JarProvider;

/**
 *
 * @author Marcin
 */
public class JarProviderImpl implements JarProvider {

    private String pathname;

    public JarProviderImpl(String pathname) {
        this.pathname = pathname;
    }

    public Object[] getJar(String name) {
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;
        try {
            File file = new File(pathname+name+".jar");
            Object[] objTab = new Object[3];
            byte[] data = new byte[(int) file.length()];
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            dis = new DataInputStream(bis);
            if (dis.read(data) == data.length) {
                objTab[0] = data;
                objTab[1] = file.getName();
                objTab[2] = CryptoUtils.md5(data);
                return objTab;
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(JarProviderImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(JarProviderImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fis.close();
            } catch (IOException ex) {
                Logger.getLogger(JarProviderImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }
}
